package com.showsu.editor.job;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.showsu.editor.entity.po.TblXhsEditorAwemeRecord;
import com.showsu.editor.entity.po.TblXhsEditorMediaRecord;
import com.showsu.editor.enums.*;
import com.showsu.editor.manage.XhsEditorAwemeBusinessManager;
import com.showsu.editor.service.*;
import com.showsu.editor.utils.AESUtil;
import com.showsu.editor.utils.BsResult;
import com.showsu.editor.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 编辑器笔记二次内容安全监测
 *
 * @author jph
 *  * @since 2024/9/24
 */
@Component
@Slf4j
public class EditorModerationSchedule {

    @Autowired
    private IXhsEditorAwemeRecordService editorAwemeRecordService;

    @Autowired
    private IXhsEditorMediaRecordService mediaRecordService;

    @Autowired
    private XhsEditorAwemeBusinessManager editorAwemeBusinessManager;

    @Autowired
    private IMailService mailService;

    @Scheduled(cron = "0 0/2 * * * ? ")
    public void execute() {


        List<TblXhsEditorAwemeRecord> awemeRecordList=editorAwemeRecordService.list(new QueryWrapper<TblXhsEditorAwemeRecord>().lambda()
                .le(TblXhsEditorAwemeRecord::getLastUpdate, DateUtil.offsetMinute(new Date(),-10))
                .eq(TblXhsEditorAwemeRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .and(wq -> wq
                        .eq(TblXhsEditorAwemeRecord::getStatus, EditorAwemeStatusEnum.Review.getCode())
                        .or().eq(TblXhsEditorAwemeRecord::getStatus, EditorAwemeStatusEnum.TextReview.getCode())
                )
        );
        String mailContent="";
        for(TblXhsEditorAwemeRecord awemeRecord :awemeRecordList){
            ModerationRiskLevelEnum riskLevelEnum=ModerationRiskLevelEnum.none;
            if(EditorAwemeStatusEnum.TextReview.getCode()==awemeRecord.getStatus()){
                String iText="";
                if(StringUtils.isNotBlank(awemeRecord.getTitle())){
                    try {
                        iText+= AESUtil.decrypt(awemeRecord.getTitle());
                    }catch (Exception ignored){
                    }
                }
                if(StringUtils.isNotBlank(awemeRecord.getContent())){
                    String content=null;
                    try {
                        content=AESUtil.decrypt((awemeRecord.getContent()));
                    }catch (Exception ignored){}
                    iText+=content;
                }
                riskLevelEnum=editorAwemeBusinessManager.textModeration(iText);
            }
            List<String> reviewImgList=new ArrayList<>();
            StringBuilder reviewImgMsg= new StringBuilder();
            if(StringUtils.isNotBlank(awemeRecord.getImageUrl())){
                List<String> imgList=null;
                try {
                    imgList=JSON.parseArray(awemeRecord.getImageUrl(),String.class);
                }catch (Exception ignored){}
                if(!CollectionUtils.isEmpty(imgList)){
                    for(String imgUrl : imgList){
                        TblXhsEditorMediaRecord mediaRecord=mediaRecordService.getOne(new QueryWrapper<TblXhsEditorMediaRecord>().lambda()
                        .eq(TblXhsEditorMediaRecord::getMediaUrl,imgUrl)
                        .eq(TblXhsEditorMediaRecord::getUserId, awemeRecord.getUserId())
                        .eq(TblXhsEditorMediaRecord::getFlag, RowFlagEnum.UnDelete.getCode()));
                        if(mediaRecord!=null){
                            ModerationStatusEnum moderationStatusEnum=ModerationStatusEnum.getByCode(mediaRecord.getStatus());
                            switch (moderationStatusEnum){
                                case ManualReview:
                                case Review:
                                    reviewImgList.add(imgUrl);
                                    reviewImgMsg.append(imgUrl).append("<br/>");
                                    break;
                            }
                        }
                    }

                }
            }

            EditorAwemeStatusEnum awemeStatusEnum=EditorAwemeStatusEnum.Enable;
            switch (riskLevelEnum){
                case none:
                    break;
                default:
                    awemeStatusEnum=EditorAwemeStatusEnum.ManualReview;
                    break;
            }
            if(!CollectionUtils.isEmpty(reviewImgList)){
                awemeStatusEnum=EditorAwemeStatusEnum.ManualReview;
                reviewImgMsg.insert(0, "图片安全检测结果：<br/>");
            }
            awemeRecord.setStatus(awemeStatusEnum.getCode());
            editorAwemeRecordService.updateById(awemeRecord);
            if(EditorAwemeStatusEnum.ManualReview.equals(awemeStatusEnum)){
                //发送人工审核邮件
                mailContent+="笔记ID:"+awemeRecord.getId()+"<br/>";
                mailContent+="文本安全检测结果："+riskLevelEnum.getMsg()+"<br/>";
                mailContent+=reviewImgMsg.toString();
                mailContent+="-------------------------<br/> <br/>";
            }
        }
        if(StringUtils.isNotBlank(mailContent)){
            mailService.sendMail("a18258468394@163.com","内容安全检测",mailContent,"红薯秀编辑器");
//            mailService.sendMail("569592809@qq.com","内容安全检测",mailContent,"红薯秀编辑器");
        }

    }
}
